import Vue from 'vue'
import VueRouter from 'vue-router'

import mainpage from '../components/mainpage.vue'
import login from '../components/login.vue'
import regest from '../components/regest.vue'
import bin from '../components/bin.vue'
import create_normal from '../components/create_normal.vue'
import message from '../components/message.vue'
import publish from '../components/publish.vue'
import question from '../components/question.vue'
import preview from "../components/preview.vue"
import report from "../components/report.vue"
import introduction from "../components/introduction.vue"
import verify from "../components/verify.vue"
import create_exam from '../components/create_exam.vue'
import exam from '../components/exam.vue'


const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
//防止重复跳转同一个路由

Vue.use(VueRouter)


const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/introduction' },
    {
      path: '/mainpage', component: mainpage, redirect: '/publish',
      children: [
        { path: '/bin', component: bin },
        { path: '/message', component: message },
        { path: '/publish', component: publish }
      ]
    },
    { path: '/login', component: login },
    { path: '/regest', component: regest },
    { path: '/create_normal', component: create_normal, },
    { path: '/question/:id', component: question },
    { path: '/preview', component: preview },
    { path: '/report', component: report },
    { path: '/verify/:id', component: verify },
    { path: '/create_exam', component: create_exam },
    { path: '/exam/:id', component: exam },
    { path: '/introduction', component: introduction },
  ]
})

// //挂载路由导航守卫
// router.beforeEach((to, from, next) => {
//   //to将要访问的路径
//   //from从哪个路径跳转来
//   //next是一个函数,表示放行
//   if (to.path === '/login') return next()
//   if (to.path === '/question/:id') return next()
//   if (to.path === '/regest') return next()
//   if (to.path === '/mainpage') return next()
//   if (store.state.isLogin === false) {
//     return next('/login')
//   }
//   next()
// })

export default router
